<?php


class svnserv {
	const SVNADMIN = '/usr/bin/svnadmin';
	const REPOPATH = '/data/repos/';
	const SVNCONFIG = '/etc/subversion';
	const HTPASSWD= '/usr/bin/htpasswd';
	const PASSWD = '/etc/httpd/conf/passwd';
	const AUTH = '/etc/httpd/conf/authz';
	const BACKPATH = '/data/www/pm_svn/_bak/conf/';
	
	public function succ($data = null){
		return array('code'=>0,'desc'=>'succ','data'=>$data);
	}
	
	public function fail($msg,$code = '-1'){
		return array('code'=>$code,'desc'=>$msg);
	}
	
    public function create($name){
    	$repo = self::REPOPATH.$name;
    	if(file_exists($repo)){
    		return $this->fail('目录已存在');
    	}
    	$op = self::SVNADMIN.' create --config-dir '.self::SVNCONFIG.' '.$repo;
    	return $this->exec($op);
    }
    
    public function adduser($user){
    	if(!file_exists(self::HTPASSWD)){
    		return $this->fail('密码文件不存在');
    	}
    	if(empty($user['user'])){
    		return $this->fail('用户不存在');
    	}
    	if(empty($user['pwd'])){
    		return $this->fail('用户未设置密码');
    	}
    	$back = self::BACKPATH.'passwd_'.date('m-d').mt_rand(1,1000);
    	copy(self::PASSWD,$back);
    	$op = sprintf(self::HTPASSWD.' -b '.self::PASSWD.' %s %s',$user['user'],$user['pwd']);
    	return $this->exec($op);
    }
    
    public function deluser($user){
    	if(!file_exists(self::HTPASSWD)){
    		return $this->fail('密码文件不存在');
    	}
    	if(empty($user['user'])){
    		return $this->fail('用户不存在');
    	}
    	$back = self::BACKPATH.'passwd_'.date('m-d').mt_rand(1,1000);
    	copy(self::PASSWD,$back);
    	$op = sprintf(self::HTPASSWD.' -D '.self::PASSWD.' %s ',$user['user']);
    	return $this->exec($op);
    }
    
    public function upAuth($auth){
    	if(!file_exists(self::AUTH)){
    		$this->fail('权限文件不存在');
    	}
    	$back = self::BACKPATH.'authz_'.date('m-d').mt_rand(1,1000);
    	copy(self::AUTH,$back);
    	$auth_old = file_get_contents(self::AUTH);
    	file_put_contents(self::AUTH, $auth);
    	return array('code'=>0,'msg'=>'succ','data'=>$auth_old);
    }
    
    public function exec($op){
    	//echo $op;
    	exec($op,$out,$ret);
    	//print_r($out);
    	//print_r($ret);
    	return array('code'=>$ret,'desc'=>$out);
    }
}

?>